package weaver.interfaces.zhlx.duss.action.financemodule.srmpmp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.SrmPMPUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 项目用款申请流程提交归档时调用推送项目中心管理平台（SRM）提供付款支付信息回传接口
 * @ModifyLog:
 **/
public class WeaXmyksqlcPMPAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaXmyksqlcPMPAction>>>>>requestid=" + requestid);
        try {
            String message = "调用SRM项目管理系统接口返回:";
            RecordSet rs = new RecordSet();
            rs.executeQuery("select id from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            JSONArray xmykList1 = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
            while (rs.next()) {
                JSONObject dataJson = new JSONObject();
                dataJson.put("planId", Util.null2String(rs.getString("zjjhxh")));
                dataJson.put("sapFiscalYear", Util.null2String(rs.getString("hjnd")));
                dataJson.put("sapAccounting", Util.null2String(rs.getString("sappzh")));
                dataJson.put("sapNo", Util.null2String(rs.getString("syjexxmbh")));
                dataJson.put("amount", Util.null2String(rs.getString("syje")));
                xmykList1.add(dataJson);
            }
            if (!xmykList1.isEmpty()) {
                JSONObject paramsJson = new JSONObject();
                paramsJson.put("fundData", xmykList1);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaXmyksqlcPMPAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaXmyksqlcPMPAction>>>>>paramsJson=" + paramsJson);
                JSONObject resultJson = SrmPMPUtil.projFundToRemainAmount(paramsJson, dataMap);
                writeLog(">>>>>WeaXmyksqlcPMPAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    JSONObject returnDataJson = JSONObject.parseObject(resultJson.getString("data"));
                    if (!"200".equalsIgnoreCase(returnDataJson.getString("code"))) {
                        message += Util.null2String(returnDataJson.getString("message"));
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            JSONArray xmykList2 = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt2 where mainid=?", mainid);
            while (rs.next()) {
                JSONObject dataJson = new JSONObject();
                dataJson.put("processId", requestid);
                dataJson.put("planId", Util.null2String(rs.getString("zjjhxh")));
                dataJson.put("payType", Util.null2String(rs.getString("fkxsmc")));
                dataJson.put("isPur", Util.null2String(rs.getString("sfgh")));
                dataJson.put("currency", Util.null2String(rs.getString("bz")));
                dataJson.put("payAmountLocal", Util.null2String(rs.getString("jyjebwb")));
                dataJson.put("payAmount", Util.null2String(rs.getString("sfje")));
                dataJson.put("payTime", Util.null2String(rs.getString("zfsj")));
                dataJson.put("payBank", Util.null2String(rs.getString("fkyx")));
                dataJson.put("payAccount", Util.null2String(rs.getString("fkzh")));
                dataJson.put("holder", Util.null2String(rs.getString("qsxx")));
                dataJson.put("secondParty", Util.null2String(rs.getString("skdw")));
                dataJson.put("bankStatementId", Util.null2String(rs.getString("yxlsid")));
                xmykList2.add(dataJson);
            }
            if (!xmykList2.isEmpty()) {
                JSONObject paramsJson = new JSONObject();
                paramsJson.put("fundData", xmykList2);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaXmyksqlcPMPAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaXmyksqlcPMPAction>>>>>paramsJson=" + paramsJson);
                JSONObject resultJson = SrmPMPUtil.projFund(paramsJson, dataMap);
                writeLog(">>>>>WeaXmyksqlcPMPAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    JSONObject returnDataJson = JSONObject.parseObject(resultJson.getString("data"));
                    if (!"200".equals(returnDataJson.getString("code"))) {
                        message += Util.null2String(returnDataJson.getString("message"));
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaXmyksqlcPMPAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}